//**************************************************************
// jQuery.large
// powerby atrl.name
// jQuery.large version 0.1
// First Release on Dec 24 2010
//**************************************************************

(function($){

	$.fn.large = function(settings){

		settings = jQuery.extend({
			maxWidth: 300,
			maxHeight: 300
		},settings);
		

		var leftSpan  = "<span class='leftSpan'><span></span></span>";
		var rightSpan = "<span class='rightSpan'><span></span></span>";
		$(this).hover(function(){
			var imageLeft   = $(this).offset().left;  
			var imageTop    = $(this).offset().top;
			var imageWidth  = $(this).width();
			var imageHeight = $(this).height();
			var newImage    = "<div class='wrapper'><img src='"+$(this).attr('src')+"' alt='"+$(this).attr('alt')+"'  /></div>";
			$(newImage).appendTo("body");
			var obj         = $(".wrapper img");
			Img(obj,settings.maxWidth,settings.maxHeight);
			//左定位
			var left=$(window).width()+$("html").scrollLeft()-imageLeft-imageWidth;
			if(left>$(".wrapper").width()){
				$(leftSpan).prependTo(".wrapper");
				$(".wrapper").css({"left":imageLeft+$(this).width()+6});
			}else{
				$(rightSpan).appendTo(".wrapper");
				$(".wrapper").css({"left":imageLeft-$(".wrapper").width()-18});
			};
			//上定位
			var top=$(window).height()+$("html").scrollTop()-imageTop;
			if(top>$(".wrapper").height()){
				$(".wrapper").css({"top":imageTop});
				$(".wrapper>span").css({"top":imageHeight/2-7});
			}else{
				$(".wrapper").css({"top":$(window).height()+$("html").scrollTop()-$(".wrapper").height()});
				var spanPosition=imageTop-($(window).height()+$("html").scrollTop()-$(".wrapper").height())+imageHeight/2-7;
				$(".wrapper>span").css({"top":spanPosition});
			};
		},function(){
			$(".wrapper").remove();	
		});
	}
})(jQuery);
//缩放
function Img(obj,maxW,maxH)
{
	var imgH=$(obj).height();
	var imgW=$(obj).width();
	if($(obj).height()>maxH)
	{
		$(obj).height(maxH);
		$(obj).width(imgW*(maxH/imgH));
		imgH=maxH;
		imgW=$(obj).width();
	}
   
	if($(obj).width()>maxW)
	{
		$(obj).width(maxW);
		$(obj).height((maxW/imgW)*imgH);
		imgW=maxW;
		imgH=$(obj).height();
	}
	$(obj).css({"marginTop":(maxH-$(obj).height())/2+'px'});
	$(obj).css({"marginLeft":(maxW-$(obj).width())/2+'px'});
}